﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using System;
using System.Drawing;
using System.Windows.Forms;
using CommonLibrary;
using CommonLibrary.Forms;

namespace SCADA
{
    /// <summary>
    /// 登录界面
    /// </summary>
    public partial class FormLogin : HWFrmWithTitle
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FormLogin()
        {
            InitializeComponent();
            this.radioButtonPassword.Checked = LoginType == ELoginType.USERNAME;
            this.radioButtonCard.Checked = LoginType == ELoginType.CARD;
            this.labelLoginImg.Font = new Font(FontHelper.GetFont(), 72);
            this.labelLoginImg.Text = FontHelper.USER;
        }

        /// <summary>
        /// 登录方式，0密码1刷卡
        /// </summary>
        private ELoginType LoginType = BusinessConfig.Instance().LoginType;
        /// <summary>
        /// 登陆按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonLoginClick(object sender, EventArgs e)
        {
            if (LoginType == 0 && (textboxUserName.Text == "" || textboxPassword.Text == ""))
            {
                lb_Msg.Text = "用户名和密码不能为空";
                return;
            }
            else if (LoginType == ELoginType.CARD && string.IsNullOrWhiteSpace(textboxCardID.Text))
            {
                lb_Msg.Text = "提示:请刷卡";
                textboxCardID.Text = "";
                textboxCardID.Focus();
                return;
            }
            UserLogin();
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        private void UserLogin()
        {
            UserInfo userInfo = new UserInfo();
            userInfo.UserName = textboxUserName.Text;
            userInfo.CreditCardID = EncryptDecryptHelper.Encrypt(textboxCardID.Text);
            userInfo.UserPassword = EncryptDecryptHelper.Encrypt(textboxPassword.Text);
            LogHelper.Info($"【start】{userInfo.ToString()}");
            lb_Msg.Text = "";
            var loginUser = UserHelper.UserLogin(LoginType, userInfo.UserName, userInfo.UserPassword, userInfo.CreditCardID);
            if (loginUser != null && loginUser.Id > 0)
            {
                LogHelper.Info($"【end】登录成功:{UserHelper.LoginUser}");
                var otherSetting = BusinessConfig.Instance();
                otherSetting.LoginType = LoginType;
                otherSetting.Save();
                if (FormMain.Instance.IsFormMain == false)
                {
                    FormMain.Instance.IsFormMain = true;
                    this.Hide();//隐藏窗体
                    this.Opacity = 0.0f;//设置窗体为透明
                    this.ShowInTaskbar = false;//不在任务栏显示
                    FormMain.Instance.ShowDialog();
                }
                else
                {
                    FormMain.Instance.LoadUser();
                    FormMain.Instance.UpdateMouseTime();
                }
                this.Close();
                return;
            }
            else
            {
                if (LoginType == ELoginType.USERNAME)
                {
                    LogHelper.Info($"【end】登录失败:{userInfo} 提示:用户名或密码错误");
                    lb_Msg.Text = "提示:用户名或密码错误";
                    textboxPassword.Text = "";
                    textboxPassword.Focus();
                }
                else
                {
                    LogHelper.Info($"【end】登录失败:{userInfo} 提示:刷卡ID不正确");
                    this.Invoke(new Action(() =>
                    {
                        lb_Msg.Text = "提示:刷卡ID不正确";
                        textboxCardID.Text = "";
                        textboxCardID.Focus();
                    }));
                }
            }
        }
        /// <summary>
        /// 用户名框按键触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_Name_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.textboxUserName.Text.Trim().Length == 0) return;
            if (e.KeyValue == 13)
            {
                textboxPassword.Focus();
            }
        }
        /// <summary>
        /// 密码框按键触发事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_Pwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.textboxPassword.Text.Trim().Length == 0) return;
            if (e.KeyValue == 13)
            {
                ButtonLoginClick(null, null);
            }
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="Exception"></exception>
        private void FormLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (FormMain.Instance.IsFormMain == false)
            {
                try
                {
                    Application.ExitThread();
                    this.Dispose();
                    Application.Exit();
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                    throw new Exception("关闭软件出错" + ex);
                }
            }
        }
        /// <summary>
        /// 初始化各个控件
        /// </summary>
        private void InitControls()
        {
            LoginType = this.radioButtonPassword.Checked ? ELoginType.USERNAME : ELoginType.CARD;
            this.textboxCardID.Text = string.Empty;
            this.textboxPassword.Text = string.Empty;
            this.textboxCardID.Enabled = LoginType == ELoginType.CARD ? true : false;
            this.textboxPassword.Enabled = LoginType == ELoginType.USERNAME ? true : false;
            this.textboxUserName.Enabled = LoginType == ELoginType.USERNAME ? true : false;
            if (LoginType == ELoginType.CARD)
            {
                LoginType = ELoginType.CARD;
                this.textboxCardID.Focus();
            }
            else
            {
                LoginType = ELoginType.USERNAME;
                this.textboxPassword.Focus();
            }
        }
        /// <summary>
        /// 窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormLogin_Load(object sender, EventArgs e)
        {
            InitControls();
        }
        /// <summary>
        /// 单选按钮值改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButtonCard_CheckedChanged(object sender, EventArgs e)
        {
            InitControls();
        }

        private void FormLogin_Shown(object sender, EventArgs e)
        {
        }

        private void FormLogin_Resize(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }
    }
}